package com.wyx.utils.commonUtil;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wyx.easyExcel.easyExcelPojo.CellStyleWY;
import okhttp3.Headers;
import okhttp3.Request;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @date 2024/03/30/9:12
 * @target 心若有所向往，何惧道阻且长
 */
public class CUtil {
    private static Gson gson = new Gson();


    public static Map<String, String> getSetCookie() {
        return null;
    }

    //    输出格式化之后的JSON字符串在控制台
    public static String JsonFormatting(String json) {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        return gson.toJson(gson.fromJson(json, Object.class));
    }

    public static String obgToJson(Object obj) {
        return gson.toJson(obj);
    }

    public static <T> T jsonToObj(String json, Class<T> objClass) {
        T t = gson.fromJson(json, objClass);
        return t;
    }

    public static Request.Builder setCookie(Request.Builder request, Map<String, String> cookieMap) {
        StringBuilder cookie = new StringBuilder();
        for (Map.Entry<String, String> cookieEntry : cookieMap.entrySet()) {
            String key = cookieEntry.getKey();
            String value = cookieEntry.getValue();
            cookie.append(key).append("=").append(value).append(";");
            System.out.println("设置的cookie为：" + key + "=" + value);
        }
        String newCookie = cookie.charAt(cookie.length() - 1) == ';' ? cookie.deleteCharAt(cookie.length() - 1).toString() : cookie.toString();
        Request.Builder requestBuilder = request.header("Cookie", newCookie);
        return requestBuilder;
    }

    /**
     * 遍历响应头中的SetCookie 将setCookie存为一个map
     *
     * @param headers 响应头
     */
    public static void set_SetCookie(Headers headers, Map<String, String> cookieMap) {
        List<String> values = headers.values("Set-Cookie");
        for (String value : values) {
            String[] cookieKeyValues = value.split(";");
            String cookieKeyValue = cookieKeyValues[0].trim();
            int index = cookieKeyValue.indexOf("=");
            String cookieKey = cookieKeyValue.substring(0, index);
            String cookieValue = cookieKeyValue.substring(index + 1);
            cookieMap.put(cookieKey, cookieValue);
        }
    }

    /**
     * 封装一个比较完善的http请求的方法  还没写完，大概就这样！！
     * 还有一个结果的类型的字节码  大概就这样
     */
    public static void sendRequest(Map<String, String> formBodyMap, Map<String, String> fromHeadMap, String url, Class c) {

    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 生成number以内的一个 0  -->  number-1  的数据集合
     *
     * @param number
     * @return
     */
    public static List<Integer> getNumberList(int number) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            list.add(i);
        }
        return list;
    }

    /**
     * 传入一个可变数组，根据i的值，给CellStyleWY赋值  如何做到TODO：
     *
     * @param i
     */
    public static void paixu(int i, CellStyleWY cellStyleWY, IndexedColors[]... s) {
        int length = s.length;
    }
}

